package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FileUtils;
import org.springblade.common.constant.LndxKTConstants;
import org.springblade.common.utils.LndxUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.app.entity.LndxFile;
import org.springblade.modules.app.service.LndxFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("blade-app/fankui")
public class LndxFileByFanKuiController extends BladeController {


    @Autowired
    private LndxFileService lndxFileService;



    @GetMapping("/fileList")
    public List<LndxFile>  fileList(LndxFile lndxFile) {
        // 每次查询前先初始化ftp文件 保存到数据库
        initFtpFileToDB();
        // 查询视频文件数据
        LambdaQueryWrapper<LndxFile> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(lndxFile.getFilename())) {
            queryWrapper.and(wrapper -> wrapper.like(LndxFile::getFilename, lndxFile.getFilename()));
        }
        if (!StringUtils.isEmpty(lndxFile.getCtime())) {
            queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getCtime, lndxFile.getCtime()));
//            queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getCtime, LndxUtils.getNowDateForyyyy_MM_dd()));
        }
        queryWrapper.orderByDesc(LndxFile::getCtime);
        List<LndxFile> lndxFiles = lndxFileService.list(queryWrapper);
        return lndxFiles;
    }



    @GetMapping("/pageBySearch")
    public R<IPage<LndxFile>> page(Page page, LndxFile lndxFile) {
        // 每次查询前先初始化ftp文件 保存到数据库
        initFtpFileToDB();
        // 查询视频文件数据
        LambdaQueryWrapper<LndxFile> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(lndxFile.getFilename())) {
            queryWrapper.and(wrapper -> wrapper.like(LndxFile::getFilename, lndxFile.getFilename()));
        }
        if (!StringUtils.isEmpty(lndxFile.getCtime())) {
            queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getCtime, lndxFile.getCtime()));
//            queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getCtime, LndxUtils.getNowDateForyyyy_MM_dd()));
        }
        queryWrapper.orderByDesc(LndxFile::getCtime);
        IPage<LndxFile> pages = lndxFileService.page(page, queryWrapper);
        return R.data(pages);
    }

    private void initFtpFileToDB(){
        String todayPath = LndxUtils.getNowDateForyyyyMMdd();
        File todayFile = new File(LndxKTConstants.FILE_PATH_RT + todayPath+"/");
        if (todayFile != null && todayFile.exists() && todayFile.isDirectory()) {
            Collection<File> fileCollection = FileUtils.listFiles(todayFile,null,true);
            for (File file : fileCollection) {
                LambdaQueryWrapper<LndxFile> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getFilename, file.getName()));
                queryWrapper.and(wrapper -> wrapper.eq(LndxFile::getFilepath, todayPath));
                List<LndxFile> list = lndxFileService.list(queryWrapper);
                if(list.isEmpty()&&list.size()==0){
                    LndxFile lndxFile = new LndxFile();
                    String  primaryKey = IdWorker.getIdStr();
                    lndxFile.setId(primaryKey);
                    lndxFile.setFilehost(LndxKTConstants.FILE_HOST);
                    //TODO 只有归档完视频 才会把filedic赋值lndx
                    lndxFile.setFiledic(null);
                    lndxFile.setFilepath(todayPath);
                    lndxFile.setFilename(file.getName());
                    lndxFile.setCtime(new Date());
                    lndxFileService.save(lndxFile);
                }
            }
        }
    }



}


